//
//  HXReadLeaveMsgTableView.m
//  HXTClient
//
//  Created by lemon on 14-4-22.
//  Copyright (c) 2014年 easaa. All rights reserved.
//

#import "HXReadLeaveMsgTableView.h"
#import "HXReadLeaveMsgTableViewCell.h"

#define HXReadLeaveMsgTableViewCellIndentifier @"HXReadLeaveMsgTableViewCellIndentifier"

@interface HXReadLeaveMsgTableView ()
{
    NSMutableArray *_contents;
}


@end

@implementation HXReadLeaveMsgTableView

- (void)dealloc
{
    HX_RELEASE(_contents)
    HX_SUPER_DEALLOC
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        
        self.delegate =self;
        self.dataSource =self;
        
    }
    return self;
}

- (void)addLeaveMsg:(NSArray *)leaveMsgs
{
    if (_contents == nil)
    {
        _contents = [NSMutableArray array];
    }
    [_contents addObjectsFromArray:leaveMsgs];
     [self reloadData];
    dispatch_main_async_safe(^{
        [self reloadData];
    });
}

- (void)addOneContent:(int)type Content:(NSString *)strContent senderName:(NSString *)strName senderTime:(NSString *)strTime
{
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
                         strContent, textContentKey,
                         [NSNumber numberWithInt:type], textTypeKey,
                         strName, textSenderNameKey,
                         strTime, textSenderTimeKey, nil];
    if (_contents == nil)
    {
        _contents = [NSMutableArray arrayWithCapacity:0];
    }
    [_contents addObject:dic];
}

- (void)reloadDataFromBottom
{
    [self insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:_contents.count - 1 inSection:0]]
                withRowAnimation:UITableViewRowAnimationBottom];
    [self scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:_contents.count - 1 inSection:0]
                atScrollPosition:UITableViewScrollPositionNone
                        animated:YES];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_contents != nil && _contents.count > 0)
        return _contents.count;
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HXReadLeaveMsgTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:HXReadLeaveMsgTableViewCellIndentifier];
    if (cell == nil)
    {
        cell = [[HXReadLeaveMsgTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                                  reuseIdentifier:HXReadLeaveMsgTableViewCellIndentifier];
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dicContent = _contents[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [HXReadLeaveMsgTableViewCell readLeaveMsgTableViewCellHeightWith:_contents[indexPath.row]];
}

@end
